/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * @author yaoyuming
 * @date  2020-07-24 15:37
 */
package com.yao.mp;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.yao.constant.Const;
import com.yao.util.CustMapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yaoyuming
 * @date 2020/07/24 15:37
 */
public class MpGenerator {

    private static final Logger log = LoggerFactory.getLogger(MpGenerator.class);


    private static final String DEFAULT_MODULE = "TEST";


    // 全局配置
    private static GlobalConfig gc(){
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java")
                .setFileOverride(true)
                .setOpen(false)
                .setAuthor(Const.AUTHOR)
                .setSwagger2(true)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setDateType(DateType.TIME_PACK)
                .setEntityName("%sEntity")
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController")
                .setIdType(IdType.AUTO);
        return gc;
    }

    // 数据源配置
    private static DataSourceConfig dsc(){
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                // jdbc:mysql://192.168.22.188:3306/fusion?useUnicode=true&useSSL=false&characterEncoding=utf8
                .setUrl(CustMapUtil.get(Const.KEY_DB_URL))
                .setUsername("root")
                .setPassword("123456")
                .setTypeConvert(new TypeConvertImpl());
        return dsc;

    }

    private static PackageConfig pc(){
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.yao")
                .setModuleName(DEFAULT_MODULE);

        return pc;
    }

    // 数据库表配置
    private static StrategyConfig strategy(){
        StrategyConfig strategy = new StrategyConfig();

        strategy.setSkipView(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setTablePrefix("sys_", "ro_")
                .setEnableSqlFilter(false)
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                .setInclude("sys_menu", "sys_user", "ro_goods");
        return strategy;
    }

    private static InjectionConfig cfg(PackageConfig pc, StrategyConfig strategy){
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();

                map.put("package", CustMapUtil.get("package"));
                setMap(map);
            }

        };

        List<FileOutConfig> focList = new ArrayList<>();

        focList.add(new FileWriteConfig("/ftl/dto_template.ftl"));
/*

        focList.add(new FileWriteConfig("/ftl/controller_template.ftl"));
        focList.add(new FileWriteConfig("/ftl/entity_template.ftl"));
        focList.add(new FileWriteConfig("/ftl/mapper_sql_template.ftl"));
        focList.add(new FileWriteConfig("/ftl/mapper_template.ftl"));
        focList.add(new FileWriteConfig("/ftl/service_impl_template.ftl"));
        focList.add(new FileWriteConfig("/ftl/service_template.ftl"));
*/


        // 如果模板引擎是 freemarker
//        String templatePath = "/templates/mapper.xml.ftl";
        // 自定义输出配置
//        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        /*focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return "D:/personalgit/temp/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
//                return System.getProperty("user.dir") + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });*/
        cfg.setFileCreate(new FileCreateImpl());


        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    private static TemplateConfig tc(){
        TemplateConfig tc = new TemplateConfig();
        tc.setController("/ftl/controller_template.ftl");
        tc.setEntity("/ftl/entity_template.ftl");
        tc.setXml("/ftl/mapper_sql_template.ftl");
        tc.setMapper("/ftl/mapper_template.ftl");
        tc.setServiceImpl("/ftl/service_impl_template.ftl");
        tc.setService("/ftl/service_template.ftl");

        return tc;
    }

    public static void excute(){
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        PackageConfig pc = pc();
        StrategyConfig strategy = strategy();

        mpg.setGlobalConfig(gc());
        mpg.setDataSource(dsc());
        mpg.setPackageInfo(pc);
        mpg.setStrategy(strategy);
        mpg.setCfg(cfg(pc, strategy));
        mpg.setTemplateEngine(new FreemarkerTemplateImpl());
        mpg.setTemplate(tc());
        mpg.execute();
    }

}